'use strict';

import axios from 'axios';
import qs from 'qs';

// const baseUrl = 'http://hotelapi.jimanke.com/';
const baseUrl = 'http://foodapi.jimanke.com/api';

axios.interceptors.request.use(config => {
  // loading
  return config;
}, error => {
  console.log('请求错误')
  return Promise.reject(error);
});

axios.interceptors.response.use(response => {
  console.log(response);
  return response;
}, error => {
  console.log(error);
  return Promise.resolve(error.response);
});

function checkStatus (response) {
  console.log(3);
  // loading
  // 如果http状态码正常，则直接返回数据
  if (response && (response.status === 200 || response.status === 304 || response.status === 400)) {
    return response;
    // 如果不需要除了data之外的数据，可以直接 return response.data
  };
  // 异常状态下，把错误信息返回去
  return {
    status: 404,
    msg: '网络异常'
  };
};

function checkCode (response) {
  console.log(2);
  if (response && (response.status === 200 || response.status === 304 || response.status === 400)) {
    return response;
    // 如果不需要除了data之外的数据，可以直接 return response.data
  };
  // 异常状态下，把错误信息返回去
  return {
    status: 404,
    msg: '网络异常'
  };
}

export default {
  upImg (url, data) {
    let formdata = new FormData();
    data.forEach(element => {
      formdata.append('file',event.target.files[0]);
      console.log(element);
    });
    return false;
    // formdata.append('file',event.target.files[0]);
    // formdata.append('action','test');
    // axios({
    //     url:'test.php',
    //     method:'post',
    //     data:formdata,
    //     headers: {'Content-Type': 'application/x-www-form-urlencoded'}
    // }).then((res)=>{console.log(res)})
    // return axios({
    //   method: 'post',
    //   baseURL: baseUrl,
    //   url,
    //   data: {
    //     token: localStorage.getItem('token'),
    //     file: data
    //   },
    //   timeout: 10000,
    //   headers: {
    //     'X-Requested-With': 'XMLHttpRequest',
    //     'Content-Type': 'multipart/form-data'
    //   }
    // }).then(
    //   (response) => {
    //     return checkStatus(response);
    //   }
    // ).then(
    //   (res) => {
    //     return checkCode(res);
    //   }
    // );
  },
  post (url, data) {
    return axios({
      method: 'post',
      baseURL: baseUrl,
      url,
      data: qs.stringify(data),
      timeout: 10000,
      headers: {
        'X-Requested-With': 'XMLHttpRequest',
        'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
      }
    }).then(
      (response) => {
        return checkStatus(response);
      }
    ).then(
      (res) => {
        return checkCode(res);
      }
    );
  },
  get (url, params) {
    return axios({
      method: 'get',
      baseURL: baseUrl,
      url,
      params, // get 请求时带的参数
      timeout: 10000,
      headers: {
        'X-Requested-With': 'XMLHttpRequest'
      }
    }).then(
      (response) => {
        return checkStatus(response);
      }
    ).then(
      (res) => {
        return checkCode(res);
      }
    );
  }
};
